<template>
  <div>
    <Dialog v-model="showDialog" :title="formData['id'] ? '编辑分类' : '新建分类'" :width="500" @confirm="submitForm"
      @close="cancel" @open="openDialog">
      <el-form :model="formData" :rules="rules" ref="formDataRef" label-width="120px">
        <el-form-item label="分类名称" prop="name">
          <el-input type="text" placeholder="请输入分类名称" clearable maxlength="30" :show-word-limit="true"
            v-model="formData.name">
          </el-input>
        </el-form-item>

        <el-form-item label="所属上级">
          <TreeSelect class="_tree_select" v-model="formData.parentId" :treeSelectData="treeSelectData"
            :expand-on-click-node="false" :check-strictly="true"
            :treeProps="{ label: 'name', disabled: ((data: any) => { return Boolean(!data.nodeType) }) }"
            placeholder="所属上级非必填" @confirm="treeSelectChange"></TreeSelect>
        </el-form-item>
        <el-form-item label="是否有下级分组" prop="exit">
          <el-radio-group v-model="formData.nodeType" :disabled="addData['id']">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注">
          <el-input type="textarea" placeholder="请输入备注" clearable maxlength="200" :show-word-limit="true"
            v-model="formData.remark">
          </el-input>
        </el-form-item>
      </el-form>
      <!-- <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="formValidate">确 定</el-button>
        </span>
      </template> -->
    </Dialog>
  </div>
</template>
<script lang="ts" setup>
import CustomerApi from "@/api/customerApi";
import TreeSelect from "@/components/TreeSelect/TreeSelect.vue";
const props = withDefaults(
  defineProps<{
    showDialog: boolean;
    treeCurrentId: string | number;
    treeSelectData: string[];
    addData: object;
  }>(),
  {
    treeCurrentId: "",
    treeSelectData: () => {
      return [];
    },
  }
);
const formDataRef = ref();
const formData = ref({
  id: "",
  name: "",
  remark: "",
  parentId: "",
  nodeType: 0,
});
const rules = ref({
  name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
});
const { showDialog, addData } = toRefs(props);
const openDialog = () => {
  if (addData.value["id"]) {
    formData.value = JSON.parse(JSON.stringify(addData.value));
  }
};

// const formValidate = () => {
//   formDataRef.value.validate((valid: any) => {
//     if (valid) {
//       addTagGroup();
//     } else {
//       console.log("error submit!!");
//       return false;
//     }
//   });
// };
const addTagGroup = async () => {
  let rs = await CustomerApi.addTagGroup(formData.value);
  if (rs.code == 200) {
    ElMessage.success(`${formData.value["id"] ? "编辑" : "添加"}分类成功`);
    confirm();
  }
};

const submitForm = async () => {
  if (treeSelectChange(formData.value.parentId)) {
    return true;
  }
  await formDataRef.value.validate((valid: any) => {
    if (valid) {
      addTagGroup();
    }
  });
};
const treeSelectChange = (val: any) => {
  console.log("[ val ]", val);
  if (val) {
    if (val == formData.value.id) {
      ElMessage.warning("所属上级不可以设置为自己");
      return true;
    }
  }
  return false;
};
const cancel = () => {
  emit("cancel");
  formDataRef.value.resetFields();
  formData.value = {
    id: "",
    name: "",
    remark: "",
    parentId: "",
    nodeType: 1,
  };
};
const confirm = () => {
  emit("confirm");
  cancel();
};
const emit = defineEmits(["confirm", "cancel"]);
</script>

<style lang="scss" scoped>
._tree_select {
  width: 100%;
}
</style>
